﻿using mscook.core.domain;
using mscook.core.services;
using mscook.mvc.core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using mscook.mvc.Areas.weblogin.Models;
using System.Linq.Expressions;
namespace mscook.mvc.Areas.weblogin.Controllers
{
    public class cookBookController : BaseController
    {
        private ICookBookService iCookBookService;
        private IImageSettingFactory iImageSettingFactory;
        private IChefService iChefService;
        private ILoginUserService iLoginUserService;

        private readonly string SectionName = "CookBook";

        public cookBookController(ICookBookService iCookBookService, IImageSettingFactory iImageSettingFactory, IChefService iChefService, ILoginUserService iLoginUserService)
            : base(iLoginUserService)
        {
            this.iCookBookService = iCookBookService;
            this.iImageSettingFactory = iImageSettingFactory;
            this.iChefService = iChefService;
            this.iLoginUserService = iLoginUserService;
        }

        public ActionResult Index(string nowPageNum, string per = "", string cookBookName = "", string chefName="")
        {
            Expression<Func<CookBook, bool>> select_expression = o => (string.IsNullOrEmpty(cookBookName) || o.Name.Contains(cookBookName))
              && (string.IsNullOrEmpty(chefName) || o.Chef.Name.Contains(chefName));

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(nowPageNum, per);
            long total = 0;
            IList<CookBook> cookBookList = iCookBookService.SelectPageListByExpressions(pageHelper.Start, pageHelper.PerValue, out total, select_expression);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);


            ViewData["chefName"] = chefName;
            ViewData["cookBookName"] = cookBookName;
            ViewData["nowPageNum"] = nowPageNum;
            ViewData["per"] = per;
            InitNavBar();
            return View(cookBookList);
        }
        [HttpGet]
        public ActionResult Add()
        {
            InitNavBar();
            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Add(CookBook CookBook, string ChefId, string CookBookTypeId, FormCollection collections,bool IsTop=false)
        {
            CookBook.Chef = iChefService.Get(ChefId);


            if (ModelState.IsValid)
            {
                CookBook.EntityIndex = IsTop ? 1 : 0;
                //CookBook.ResourceBonusSetting.Bonus = -Math.Abs(CookBook.ResourceBonusSetting.Bonus);2013-11-29 JohnHash 用Abs做显示与赋值
                SaveResourceType(CookBook, CookBookTypeId);
                SaveRelativeInfo(collections, CookBook.Id, SectionName);
                SaveUploadImage(CookBook);
                iCookBookService.SaveOrUpdate(CookBook);
                return RedirectToIndex();
            }

            return View();
        }

        [AcceptVerbs(HttpVerbs.Get)]

        public ActionResult Edit(string id)
        {
            CookBook cookBook = iCookBookService.Get(id);
            if (cookBook != null)
            {
                CookBookModel cookBookModel = new CookBookModel() { CookBook = cookBook };
                cookBookModel.ResourceTypes = GetResourceType(cookBook.Id);
                cookBookModel.RelativeInfos = GetRelativeInfo(cookBook.Id);

                InitNavBar();
                return View(cookBookModel);
            }
            return RedirectToIndex();
        }


        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        public ActionResult Edit(string id, string ChefId, string CookBookTypeId, FormCollection collection,bool IsTop=false)
        {
            CookBookModel cookBookModel = new CookBookModel();

            //CookBook cookBook = iCookBookService.Get(id);2013-11-29 Lampard Mvc依据参数Name寻找参数绑定
            cookBookModel.CookBook = iCookBookService.Get(id);
            Chef chef = iChefService.Get(ChefId);
            if (ModelState.IsValid)
            {
                cookBookModel.CookBook.EntityIndex = IsTop ? 1 : 0;
                SaveResourceType(cookBookModel.CookBook, CookBookTypeId);
                SaveUploadImage(cookBookModel.CookBook);
                SaveRelativeInfo(collection, cookBookModel.CookBook.Id, SectionName);
                UpdateModel(cookBookModel);

                //cookBook.ResourceBonusSetting.Bonus = -Math.Abs(cookBook.ResourceBonusSetting.Bonus);
                cookBookModel.CookBook.Chef = chef;
                iCookBookService.SaveOrUpdate(cookBookModel.CookBook);
                return RedirectToIndex();
            }
            return View(cookBookModel);
        }



        public ActionResult Delete(string id)
        {
            iCookBookService.Delete(id);
            return RedirectToIndex();
        }

        public ActionResult CookbookModal(string nameInput, string idInput, string cookBookName, string linkBtn = "", string num = "")
        {

            PageBreakHelper pageHelper = new PageBreakHelper();
            pageHelper.Init(num, "3");
            long total = 0;
            IList<CookBook> cookBooklList = iCookBookService.SelectPageList(pageHelper.Start, pageHelper.PerValue, out total, cookBookName);
            pageHelper.SetViewDataKeys(ViewData, ViewBag, total);
            ViewData["nameInput"] = nameInput;
            ViewData["idInput"] = idInput;
            ViewData["linkBtn"] = linkBtn;

            return View(cookBooklList);
        }



        private bool SaveUploadImage(CookBook CookBook)
        {
            if (Request.Files.Count > 0)
            {
                HttpPostedFileBase file = Request.Files.Get(0);
                if (file.ContentLength != 0)
                {
                    CookBook.ImageName = iImageSettingFactory.CreateBatchSizedImage(SectionName, file.FileName, file.InputStream);
                }
                return true;
            }
            return false;
        }
    }
}
